using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Configuration;
using Kaskelot.SP.Core;

namespace Kaskelot.SP.UI.Features.Groups
{
	/// <summary>
	/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
	/// </summary>
	/// <remarks>
	/// The GUID attached to this class may be used during packaging and should not be modified.
	/// </remarks>

	[Guid("ab586c8e-04a5-4e00-8478-fd3f6f4821df")]
	public class GroupsEventReceiver : SPFeatureReceiver
	{
		// Uncomment the method below to handle the event raised after a feature has been activated.

		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			SPWeb root = properties.Feature.Parent as SPWeb;
			SPGroupCollection collGroups = root.SiteGroups;

			try
			{
                using (var v = root.Webs["Administration"])
                {
                    v.BreakRoleInheritance(true);
                    AddRights(v, Constants.Groups.Administrators, SPRoleType.Contributor);
                }
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.UI, "FeatureActivated", "Fel vid aktivering av funktionen Groups", ex);
			}

			try
			{
				AddRights(root, Constants.Groups.Administrators, SPRoleType.Contributor);
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.UI, "FeatureActivated", "Fel vid aktivering av funktionen Groups", ex);
			}

			try
			{
				AddRights(root, Constants.Groups.Workers, SPRoleType.Contributor);
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.UI, "FeatureActivated", "Fel vid aktivering av funktionen Groups", ex);
			}

			try
			{
				AddRights(root, Constants.Groups.Readers, SPRoleType.Reader);
			}
			catch (Exception ex)
			{
				Logger.LogError(this, LayerNames.UI, "FeatureActivated", "Fel vid aktivering av funktionen Groups", ex);
			}
		}

		private void AddRights(SPWeb web, string group, SPRoleType roleType)
		{
			SPUser oUser = web.Users[0];
			SPMember oMember = web.Users[0];
            if (!web.ContainsSiteGroup(group))
			    web.SiteGroups.Add(group, oMember, oUser, String.Empty);
            if (!web.IsPrincipalInRole(web.SiteGroups[group], web.RoleDefinitions.GetByType(roleType)))
            {
                SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteGroups[group]);
                roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(roleType));
                web.RoleAssignments.Add(roleAssignment);
                web.Update();
            }			
		}


		// Uncomment the method below to handle the event raised before a feature is deactivated.

		//public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		//{
		//}


		// Uncomment the method below to handle the event raised after a feature has been installed.

		//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
		//{
		//}


		// Uncomment the method below to handle the event raised before a feature is uninstalled.

		//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
		//{
		//}

		// Uncomment the method below to handle the event raised when a feature is upgrading.

		//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
		//{
		//}
	}
}
